/**
 * 
 */
package systole.view.crud.patient.patientComponents.familyBackground;

import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Family;
import systole.domain.clinicalInformation.FamilyPatientBackground;
import systole.domain.clinicalInformation.FamilyPatientBackgroundId;

import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.patient.patientComponents.controller.ControllerEditionPatienComponent;

/**
 * @author jmj
 *
 */
public class ControllerEditionFamilyBackground extends ControllerEditionPatienComponent {

    /**
     * @param patient
     */
    public ControllerEditionFamilyBackground(Patient patient) {
        super(patient);
        this.entityName = "Antecedente familiar";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        JDialogFamilyBackground form = (JDialogFamilyBackground) this.editForm;
        FamilyPatientBackground familyPatientPathology = (FamilyPatientBackground) this.curretnEntity;
        familyPatientPathology.setFamily((Family) form.getCmbRelatives().getSelectedItem());
        familyPatientPathology.setBackgroundCardiac(form.getjChkCardiac().isSelected());
        familyPatientPathology.setBackgroundCholesterol(form.getjChkCholesterol().isSelected());
        familyPatientPathology.setBackgroundDiabetic(form.getjChkDiabetic().isSelected());
        familyPatientPathology.setBackgroundHypertension(form.getjChkHypertension().isSelected());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogFamilyBackground form = (JDialogFamilyBackground) this.editForm;
        FamilyPatientBackground familyPatientBackground = (FamilyPatientBackground) this.curretnEntity;
        form.getCmbRelatives().setSelectedItem(familyPatientBackground.getFamily());
        form.getjChkCardiac().setSelected((familyPatientBackground.getBackgroundCardiac() == null) ? false : familyPatientBackground.getBackgroundCardiac().booleanValue());
        form.getjChkCholesterol().setSelected((familyPatientBackground.getBackgroundCholesterol() == null) ? false : familyPatientBackground.getBackgroundCholesterol().booleanValue());
        form.getjChkDiabetic().setSelected((familyPatientBackground.getBackgroundDiabetic() == null) ? false : familyPatientBackground.getBackgroundDiabetic().booleanValue());
        form.getjChkHypertension().setSelected((familyPatientBackground.getBackgroundHypertension() == null) ? false : familyPatientBackground.getBackgroundHypertension().booleanValue());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#save()
     */
    @Override
    protected void save() {
        if (!this.isEditing()) {
            FamilyPatientBackground background = (FamilyPatientBackground) this.curretnEntity;
            background.setId(new FamilyPatientBackgroundId(background.getFamily().getId(), background.getPatient().getId()));
            this.patient.getFamilyPatientBackgrounds().add(background);
        }
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        if (!this.isEditing()) {

            JDialogFamilyBackground form = (JDialogFamilyBackground) this.editForm;
            if ((form.getCmbRelatives().getSelectedIndex() == -1) || (form.getCmbRelatives().getSelectedItem() == null)) {
                return "Debe seleccionar un familiar";
            }

            try {
                Family family = (Family) form.getCmbRelatives().getSelectedItem();

                if (this.facadeDB.getFamilyBackgroundBroker().existFamilyPatientBackground(family, this.patient)) {
                    return "Ya fueron cargados los antecedentes del familiar";
                }
            } catch (ExceptionDAO ex) {
                this.facadeDB.refreshSession();
                return ex.getMessage();
            }
        }
        return null;

    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new FamilyPatientBackground(this.patient);
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogFamilyBackground(parent, this);
    }

    @Override
    protected void initFormsControls() throws ExceptionDAO {
        JDialogFamilyBackground form = (JDialogFamilyBackground) this.editForm;

        form.getCmbRelatives().removeAllItems();
        List<Family> list = this.facadeDB.getFamilyBroker().getAllFamilies();
        Iterator<Family> it = list.iterator();
        while (it.hasNext()) {
            form.getCmbRelatives().addItem(it.next());
        }
         form.getCmbRelatives().setEnabled(!this.isEditing());
        this.editForm = form;
    }
}
